//Keyboard constructor
function Keyboard(world) {
	this.World = world;
	
	//Initialize Menu Specific Vars
	this.Attributes = 0;
	this.Stat = "";
	this.Skill=0;
}

Keyboard.prototype.listener = function(e) {
	var e=window.event || e;
	var key=String.fromCharCode(e.charCode);
	
	//Determine how to handle the input key.
	switch(this.World.Flags) {
		case 0:
			this.World.Menu.keyListener(key);
			break;
		case 1:
			this.openClose(key,1);
			break;
		case 2:
			this.openClose(key,0);
			break;
		case 3:
			this.World.Menu.keyListener(key);
			break;
		case 4:
			this.skills(key);
			break;
		case 5:
			this.World.Menu.keyListener(key);
			break;
		default:
			this.input(key);
	}
}

Keyboard.prototype.skills = function(key) {
	var msg="";
	
	switch(key) {
		case "8":
			this.Skill--;
			break;
		case "2":
			this.Skill++;
			break;
		case "5":
			//Add a Skill
			break;
		case "`":
			this.World.Flags=-1;
			break;
	}
	if (this.Skill < 0) this.Skill=0;
	if (this.Skill >= this.World.Skills.List.length) this.Skill = this.World.Skils.List.length-1;
	
	this.World.Message.skills(msg,this.World.Skills.menu(this.World.Player,this.Skill));
}

Keyboard.prototype.openClose = function(key,open) {
	var nodoor=0;
	switch(key) {
		case "6":
		case "9":
		case "7":
		case "4":
		case "1":
		case "3":
			nodoor = this.World.Player.Section.openClose(key,open);
			break;
		default:
			this.World.Message.addMsg("That is not a direction.");
	}
	
	if (nodoor==1) this.World.Message.addMsg("Nothing to open in that direction.");
	else if (nodoor==2) this.World.Message.addMsg("Nothing to close in that direction.");
	else this.World.Display.refresh();
	
	this.World.Flags=-1;
}

Keyboard.prototype.input = function(key) {
	switch(key) {
		case "7":
			this.World.Player.move(-1,0);
			break;
		case "9":
			this.World.Player.move(-1,1);
			break;
		case "6":
			this.World.Player.move(0,1);
			break;
		case "3":
			this.World.Player.move(1,1);
			break;
		case "1":
			this.World.Player.move(1,0);
			break;
		case "4":
			this.World.Player.move(0,-1);
			break;
		case "5":
			if (this.World.Player.onExit()) {
				//alert("On Exit");
				this.World.genSection()
				this.World.Player.moveSection();
			}
			break;
		case "0":
			this.World.Menu.mainMenu();
			this.World.Flags=0;
			break;
		case "s":
			this.World.Menu.attributeMenu();
			this.World.Flags=5;
			break;
		case "#":
			alert("Extended Input");
			break;
		case "o":
			this.World.Flags=1;
			break;
		case "c":
			this.World.Flags=2;
			break;
		case "y":
			//alert(this.World.Player.yPos + "," + this.World.Player.xPos);
			//this.World.Player.Section.BLOS([this.World.Player.yPos,this.World.Player.xPos],[12,40],function(sec,cur) {alert("X: "+cur[1]+" Y: "+cur[0]);});
			var path = this.World.AStar.search([this.World.Player.yPos,this.World.Player.xPos],[12,40],this.World.Player.Section);
			var p="";
			for (var i=0;i<path.length;i++) {
				this.World.Player.Section.Map[path[i][0]][path[i][1]][0][0] = 5;
			}
			this.World.Player.Section.updateDisplay();
			break;
		case "*":
			alert(this.World.Player.yPos + "," + this.World.Player.xPos);
			break;
		case "S":
			this.World.Flags=4;
			this.World.Message.skills("",this.World.Skills.menu(this.World.Player,this.Skill))
			break;
		case String.fromCharCode(13):
			alert("Enter");
			break;
		default:
			alert("Key not Mapped!!! ["+key+"]");
	}
	this.World.Display.refresh("Map");
}